package com.lezhi.socialgram.util;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * Created by daizhen on 2017/3/13.
 */
public class XUtil {

    public static String getTableNameIndexByTel(String telphone) {
        String endNumber = telphone.substring(telphone.length() - 1);
        return getIndex(endNumber);
    }

    public static String getTableNameIndexById(long id) {
        String sid = Long.toString(id);
        String endNumber = sid.substring(sid.length() - 1);
        return getIndex(endNumber);
    }

    public static int getAge(Date birthDay) {
        try {
            Calendar cal = Calendar.getInstance();

            if (cal.before(birthDay)) {
                throw new IllegalArgumentException(
                        "The birthDay is before Now.It's unbelievable!");
            }

            int yearNow = cal.get(Calendar.YEAR);
            int monthNow = cal.get(Calendar.MONTH) + 1;
            int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

            cal.setTime(birthDay);
            int yearBirth = cal.get(Calendar.YEAR);
            int monthBirth = cal.get(Calendar.MONTH);
            int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

            int age = yearNow - yearBirth;

            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    //monthNow==monthBirth
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        age--;
                    }
                } else {
                    //monthNow>monthBirth
                    age--;
                }
            }

            return age;
        } catch (Exception e) {
            return 20;
        }
    }

    private static Random random = new Random(System.currentTimeMillis());

    public static int getARandomInt(int max) {
        return random.nextInt(max);
    }

    public static final String getRemoteAddr(HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        if ("127.0.0.1".equals(ip)) {
            ip = request.getHeader("x-forwarded-for");
            if (StringUtils.isEmpty(ip)) {
                return XUtil.getARandomInt(254) + "." + XUtil.getARandomInt(254) + "." + XUtil.getARandomInt(254) + "." + XUtil.getARandomInt(254);
            } else {
                String[] ips = ip.split(",");
                if (ips.length > 0) {
                    String temp = ips[0];
                    if (!"127.0.0.1".equals(temp) && temp.length() < 20) {
                        return temp;
                    } else if (ips.length > 1) {
                        return ips[1].trim();
                    } else {
                        return ip.substring(0, ip.length() > 20 ? 19 : ip.length() - 1);
                    }

                }

                return ip.substring(0, ip.length() > 20 ? 19 : ip.length() - 1);
            }
        }
        return ip;
    }

    public static final String[] constellationArr = { "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };

    public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };

    /**
     * 根据日期获取星座
     * @return
     */
    public static String getConstellation(Date date) {
        if (date == null) {
            return "";
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        if (day < constellationEdgeDay[month]) {
            month = month - 1;
        }
        if (month >= 0) {
            return constellationArr[month];
        }
        // default to return 魔羯
        return constellationArr[11];
    }

    /**
     * 生成随即纯数字密码
     *
     * @param pwd_len 生成的密码的总长度
     * @return 密码的字符串
     */
    public static String genRandomNumPassword(int pwd_len) {
        //35是因为数组是从0开始的，26个字母+10个数字
        final int maxNum = 10;
        int i;  //生成的随机数
        int count = 0; //生成的密码的长度
        char[] str = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < pwd_len) {
            //生成随机数，取绝对值，防止生成负数，

            i = Math.abs(r.nextInt(maxNum));  //生成的数最大为36-1

            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }

        return pwd.toString();
    }


    private static String getIndex(String endNumber) {
        String index = "0";
        switch (endNumber) {
            case "0":
                index = "0";
                break;
            case "1":
                index = "1";
                break;
            case "2":
                index = "2";
                break;
            case "3":
                index = "3";
                break;
            case "4":
                index = "4";
                break;
            case "5":
                index = "5";
                break;
            case "6":
                index = "6";
                break;
            case "7":
                index = "7";
                break;
            case "8":
                index = "8";
                break;
            case "9":
                index = "9";
                break;
            default:
                index = "0";
        }
        return index;
    }




}
